package service

import (
	//"bytes"
	"fmt"
	"mytest/kitcli/user/pkg/transport/grpc/pb"
	"strconv"
)

var OperatorMap = map[pb.Operator]string{
	pb.Operator_GT:    " > ",
	pb.Operator_EQUAL: " = ",
	pb.Operator_LT:    " < ",
	pb.Operator_NEQ:   " != ",
}

func GenerateQueryCondition(conditions []*pb.Query) string {
	var condition string
	for k, v := range conditions {
		if k > 0 {
			condition += " and "
		}
		//bool string int
		_, err := strconv.ParseBool(v.Value)
		if err != nil {
			condition += fmt.Sprintf("%v%s'%v'", v.Key, OperatorMap[v.Operator], v.Value)
		} else {
			condition += fmt.Sprintf("%v%s%v", v.Key, OperatorMap[v.Operator], v.Value)
		}

	}

	return condition
}

func GetTotalPage(total int, pageSize uint32) uint32 {
	totalPage := uint32(total) / pageSize
	if uint32(total)%pageSize != 0 {
		totalPage += 1
	}
	return totalPage
}
